package org.eclipse.soc.ide4edu.assignment.views;

import java.io.IOException;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Path;
import org.eclipse.soc.ide4edu.assignment.editors.AssignmentEditor;
import org.eclipse.soc.ide4edu.assignment.editors.BufferStore;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.forms.widgets.ExpandableComposite;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;
import org.eclipse.ui.part.ViewPart;

public class AssignmentView extends ViewPart {

	private FormToolkit toolkit;
	IProject assignment = null;

	IWorkbenchWindow window;

	BufferStore details = null;
	BufferStore requirements = null;
	BufferStore evaluation = null;
	BufferStore references = null;
	String assignmentName = null;

	private IMemento mem = null;
	final int BUFFER_LENGTH = 1024;

	public AssignmentView() {

	}

	@Override
	public void createPartControl(Composite parent) {

		setHandle();

		try {
			if (assignment == null) {

				if (assignmentName != null) {
					assignment = ResourcesPlugin.getWorkspace().getRoot()
							.getProject(assignmentName);
					createSections(parent);
				} else
					showNull(parent);
			} else
				createSections(parent);

		} catch (CoreException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	private void createSections(Composite composite) throws CoreException,
			IOException {

		toolkit = new FormToolkit(getViewSite().getShell().getDisplay());

		if (mem == null)
			setBuffers();

		GridLayout layout = new GridLayout();
		GridData gd = new GridData(GridData.FILL_BOTH);
		composite.setLayoutData(gd);
		composite.setLayout(layout);

		layout = new GridLayout();
		Section detailsSection = toolkit.createSection(composite,
				ExpandableComposite.TWISTIE | ExpandableComposite.TITLE_BAR);
		detailsSection.setText("Details");
		detailsSection.setLayout(layout);
		gd = new GridData(GridData.FILL_BOTH);
		StyledText detailsText = new StyledText(detailsSection, SWT.MULTI
				| SWT.READ_ONLY | SWT.V_SCROLL);

		initEdit(detailsText, details);

		detailsSection.setClient(detailsText);
		detailsSection.setLayoutData(gd);
		detailsText.setLayoutData(gd);

		Section requirementSection = toolkit.createSection(composite,
				ExpandableComposite.TWISTIE | ExpandableComposite.TITLE_BAR);
		requirementSection.setText("Functional Requirements");
		requirementSection.setLayout(layout);
		gd = new GridData(GridData.FILL_BOTH);
		StyledText requirementsText = new StyledText(requirementSection,
				SWT.MULTI | SWT.READ_ONLY | SWT.V_SCROLL);

		initEdit(requirementsText, requirements);

		requirementSection.setClient(requirementsText);
		requirementSection.setLayoutData(gd);
		requirementsText.setLayoutData(gd);

		Section evaluationSection = toolkit.createSection(composite,
				ExpandableComposite.TWISTIE | ExpandableComposite.TITLE_BAR);
		evaluationSection.setText("Evaluation Criteria");
		evaluationSection.setLayout(layout);
		gd = new GridData(GridData.FILL_BOTH);
		StyledText evaluationText = new StyledText(evaluationSection, SWT.MULTI
				| SWT.READ_ONLY | SWT.V_SCROLL);

		initEdit(evaluationText, evaluation);

		evaluationSection.setClient(evaluationText);
		evaluationSection.setLayoutData(gd);
		evaluationText.setLayoutData(gd);

		Section referenceSection = toolkit.createSection(composite,
				ExpandableComposite.TWISTIE | ExpandableComposite.TITLE_BAR);
		referenceSection.setText("References");
		referenceSection.setLayout(layout);
		gd = new GridData(GridData.FILL_BOTH);
		StyledText referencesText = new StyledText(referenceSection, SWT.MULTI
				| SWT.READ_ONLY | SWT.V_SCROLL);

		initEdit(referencesText, references);

		referenceSection.setClient(referencesText);
		referenceSection.setLayoutData(gd);
		referencesText.setLayoutData(gd);

		saveStylesInfo();
	}

	@Override
	public void dispose() {
		toolkit.dispose();
	}

	@Override
	public void init(IViewSite site, IMemento memento) {
		try {
			init(site);
			this.mem = memento;

			details = new BufferStore();
			details.setBuffer(memento.getString("DETAILS"));

			requirements = new BufferStore();
			requirements.setBuffer(memento.getString("REQUIREMENTS"));

			evaluation = new BufferStore();
			evaluation.setBuffer(memento.getString("EVALUATION"));

			references = new BufferStore();
			references.setBuffer(memento.getString("REFERENCES"));

			assignmentName = memento.getString("ASSIGNMENTNAME");

		} catch (Exception e) {
			return;
		}
	}

	private void initEdit(StyledText edit, BufferStore buffStore) {

		try {

			StyleRange[] styles = buffStore.getStyleRanges(getSite().getShell()
					.getDisplay());

			String text = buffStore.getText();
			edit.setText(text);

			if (styles != null)
				edit.setStyleRanges(styles);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	@Override
	public void saveState(IMemento memento) {

		if (details != null) {
			memento.putString(new String("DETAILS"), details.getBuffer());
		}
		if (requirements != null) {
			memento.putString(new String("REQUIREMENTS"), requirements
					.getBuffer());
		}
		if (evaluation != null) {
			memento.putString(new String("EVALUATION"), evaluation.getBuffer());
		}
		if (references != null) {
			memento.putString(new String("REFERENCES"), references.getBuffer());
		}
		if (references != null) {
			memento.putString(new String("ASSIGNMENTNAME"), assignmentName);
		}
	}

	private void saveStylesInfo() {

	}

	private void setBuffers() {
		details = new BufferStore(assignment.getFile(new Path(
				"//Details//info.asg")));
		requirements = new BufferStore(assignment.getFile(new Path(
				"//Details//desc.asg")));
		evaluation = new BufferStore(assignment.getFile(new Path(
				"//Details//eval.asg")));
		references = new BufferStore(assignment.getFile(new Path(
				"//Details//refer.asg")));
	}

	@Override
	public void setFocus() {

	}

	private void setHandle() {
		try {
			IEditorPart editor = PlatformUI.getWorkbench()
					.getActiveWorkbenchWindow().getActivePage()
					.getActiveEditor();

			if (editor instanceof AssignmentEditor) {
				IEditorInput editorin = editor.getEditorInput();
				if (editorin instanceof IFileEditorInput) {
					assignment = ((IFileEditorInput) editorin).getFile()
							.getProject();
				}
				assignmentName = assignment.getName();
			}
		} catch (NullPointerException e) {
			assignment = null;
			return;
		}
	}

	private void showNull(Composite parent) {

		toolkit = new FormToolkit(getViewSite().getShell().getDisplay());
		toolkit.createText(parent, "Project not in Focus or Null");
	}
}